﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using AWZhome.NQ.Core.Fluent;

namespace AWZhome.NQ.CommonServices.Visual
{
	/// <summary>
	/// Interaction logic for UpdateCompatibilityWarnWindow.xaml
	/// </summary>
	public partial class UpdateCompatibilityWarnWindow : Window
	{
		public UpdateCompatibilityWarnWindow()
		{
			InitializeComponent();
		}

		public UpdateCompatibilityWarnWindow(ReleaseListEntry releaseListEntry, UpdateCompatibilityException ex)
			: this()
		{
			if ((ex.CheckResults != null) && (ex.CheckResults.Length > 0))
			{
				// Show compatibility warning dialog
				IUpdateCondition[] missingConditions = ex.CheckResults[0].MissingConditions ?? new IUpdateCondition[0];
				string[] conflictConditions = ex.CheckResults[0].IncompatibleReleases ?? new string[0];
				this.MissingList = missingConditions.Select(cond => cond.Name + " " + cond.Version.ToString()).ToArray();
				this.ConflictList = conflictConditions;
			}

			this.ReleaseListEntry = releaseListEntry;
			this.DataContext = this;

			// Show only the list, that contains elements
			if ((this.ConflictList == null) || (this.ConflictList.Length == 0))
			{
				((Panel) this.conflictListGrid.Parent).Children.Remove(this.conflictListGrid);
				this.uniformGrid.Rows = 1;
			}
			if ((this.MissingList == null) || (this.MissingList.Length == 0))
			{
				((Panel) this.missingListGrid.Parent).Children.Remove(this.missingListGrid);
				this.uniformGrid.Rows = 1;
			}

			IResourceManager resService = ServiceCore
				.Get<Services.IResourceService>()
				.To;
			this.Title = resService.GetResString("NQCS", "UpdateDialog.CompatWarning.Title", releaseListEntry.Title);
		}

		public ReleaseListEntry ReleaseListEntry
		{
			get;
			private set;
		}

		public string[] MissingList
		{
			get;
			private set;
		}

		public string[] ConflictList
		{
			get;
			private set;
		}

		private void buttonOK_Click(object sender, RoutedEventArgs e)
		{
			this.Close();
		}
	}
}
